//V0.1 2023.11.29

#include "selffun.h"


quint8 selffun::hight_bit(quint8 a,quint8 b) //从高位开始取出a中的b个位数
{
    quint8 box;
    box=a;
    b=8-b;
    box >>= b;
    return box;

}

quint8 selffun::low_bit(quint8 a,quint8 b)//从低位开始取出a中的b个位数
{
    quint8 box;
    box=a;
    b=8-b;
    box <<= b;
    box >>= b;
    return box;

}

quint16 selffun::com_2uint8(quint8 a, quint8 b) //a在高位与b组合成16位
{
    quint16 box;
    box=a;
    box <<=8;
    box= box | b;
    return box;

}


quint32 selffun::com_4uint8(quint8 a, quint8 b,quint8 c,quint8 d) //a在高位与b,c,d组合成32位，0xabcd
{
    quint32 box;
    quint32 box1;
    quint32 box2;
    quint32 box3;
    quint32 box4;
    box1=a;
    box2=b;
    box3=c;
    box4=d;
    box1 <<=24;
    box2 <<=16;
    box3 <<=8;
    box= box1|box2|box3|box4;
    return box;

}

quint16 selffun::TakeOut_bit(quint8 a, quint8 b) //取a的第b位 b从0开始
{
    quint8 box;
    box=a;
    box <<=(7-b);
    box>>=7;
    return box;

}


quint8  selffun::hight16_hi(quint16 a) //取a的高8位
{
    quint8 box;
    box=a>>8;

    return box;

}

quint8  selffun::hight16_low(quint16 a) //取a的低8位
{
    quint8 box;
    quint16 box_mid;
    box_mid=a<<8;
    box_mid>>=8;
    box=box_mid;
    return box;
}

quint8  selffun::hight24_low(quint32 a) //取a的低8位
{
    quint8 box;
    box=a&0xff;
    return box;
}
quint8  selffun::hight24_mid(quint32 a) //取a的中间8位
{
    quint8 box;
    quint16 box_mid;
    box_mid=a&0xff00;
    box=box_mid>>8;
    return box;
}
quint8  selffun::hight24_hi(quint32 a) //取a的高8位
{
    quint8 box;
    quint32 box_mid;
    box_mid=a&0xff0000;
    box=box_mid>>16;
    return box;
}

quint8  selffun::hight32_25_32(quint32 a) //取32位a的第25至32位
{
    quint8 box;
    quint32 box_mid;
    box_mid=a&0xff000000;
    box=box_mid>>24;
    return box;
}


quint8  selffun::hight32_0_7(quint32 a) //取32位a的第0至7位
{
    quint8 box;
    box=a&0xff;
    return box;
}
quint8  selffun::hight32_8_15(quint32 a) //取32位a的第8至15位
{
    quint8 box;
    quint16 box_mid;
    box_mid=a&0xff00;
    box=box_mid>>8;
    return box;
}
quint8  selffun::hight32_16_23(quint32 a) //取32位a的第16至32位
{
    quint8 box;
    quint32 box_mid;
    box_mid=a&0xff0000;
    box=box_mid>>16;
    return box;
}

quint8  selffun::hight32_24_31(quint32 a) //取32位a的第24至32位
{
    quint8 box;
    quint32 box_mid;
    box_mid=a&0xff000000;
    box=box_mid>>24;
    return box;
}

quint8  selffun::hid_quint8_1(quint8 data,quint8 par)//将data的第par位置1，从0开始
{
    quint8 box= data|(1<<par);
    return box;



}
quint8  selffun::hid_quint8_0(quint8 data,quint8 par)//将data的第par位置0，从0开始
{
    quint8 box= data&(~(1<<par));
    return box;
}

quint8 selffun::PutIn_Data(quint8 data,bool val,quint8 post)//将val的值放到data的第post位
{
    if(val==0)
    {
        quint8 box= data&(~(1<<post));
        return box;
    }
    else
    {
        quint8 box= data|(1<<post);
        return box;
    }
}



selffun::Const_R134a_Physical_property PP_R134a = {

    /* R134a 包和液体和饱和气体性质表
     * 温度
     */
    { -100.0F, -90.0F, -80.0F, -70.0F, -60.0F, -50.0F, -40.0F, -30.0F, -28.0F,
     -26.0F, -24.0F, -22.0F, -20.0F, -18.0F, -16.0F, -14.0F, -12.0F, -10.0F,
     -8.0F, -6.0F, -4.0F, -2.0F, 0.0F, 2.0F, 4.0F, 6.0F, 8.0F, 10.0F, 12.0F,
     14.0F, 16.0F, 18.0F, 20.0F, 22.0F, 24.0F, 26.0F, 28.0F, 30.0F, 32.0F, 34.0F,
     36.0F, 38.0F, 40.0F, 42.0F, 44.0F, 46.0F, 48.0F, 50.0F, 52.0F, 54.0F, 56.0F,
     58.0F, 60.0F, 62.0F, 64.0F, 66.0F, 68.0F, 70.0F, 72.0F, 74.0F, 76.0F, 78.0F,
     80.0F, 85.0F, 90.0F, 95.0F, 100.0F },

    /*  R134a 包和液体和饱和气体性质表
   *  压力
   */
    { 0.00056F, 0.00152F, 0.00367F, 0.00798F, 0.01591F, 0.02945F, 0.05121F,
     0.08438F, 0.0927F, 0.10167F, 0.1113F, 0.12165F, 0.13273F, 0.1446F, 0.15728F,
     0.17082F, 0.18524F, 0.2006F, 0.21693F, 0.23428F, 0.2568F, 0.27217F, 0.2928F,
     0.31462F, 0.33766F, 0.36198F, 0.38761F, 0.41461F, 0.44301F, 0.47288F,
     0.50425F, 0.53718F, 0.57171F, 0.60789F, 0.64578F, 0.68543F, 0.72688F,
     0.7702F, 0.81543F, 0.86263F, 0.91185F, 0.96315F, 1.0166F, 1.0722F, 1.1301F,
     1.1903F, 1.2529F, 1.3179F, 1.3854F, 1.4555F, 1.5282F, 1.6036F, 1.6818F,
     1.7628F, 1.8467F, 1.9337F, 2.0237F, 2.1168F, 2.2132F, 2.3103F, 2.4161F,
     2.5228F, 2.6332F, 2.9258F, 3.2442F, 3.5912F, 3.9724F }
};


double selffun::query_R134a(float u0, const double bp0[], const double table[], quint32 maxIndex)
{


    //用法：Evaporation_temperature=query_R134a((Ars_INFO.Pressure_IN+101.325)/1000,PP_R134a.R1340_Pre,PP_R134a.R1340_Temp, 66U);
    // 冷凝温度/蒸发温度由冷凝器出压力查R134a相表获得 Evaporation_temperature冷凝温度/蒸发温度值
    // (Ars_INFO.Pressure_IN+101.325)/1000当前压力；  PP_R134a.R1340，PP_R134a.R1340_Temp物性；  66U 长整型
    float frac;
    quint32 iRght;
    quint32 iLeft;
    quint32 bpIdx;

    /* Column-major Lookup 1-D
     Search method: 'binary'
     Use previous index: 'off'
     Interpolation method: 'Linear point-slope'
     Extrapolation method: 'Linear'
     Use last breakpoint for index at or above upper limit: 'off'
     Remove protection against out-of-range input in generated code: 'off'
   */
    /* Prelookup - Index and Fraction
     Index Search method: 'binary'
     Extrapolation method: 'Linear'
     Use previous index: 'off'
     Use last breakpoint for index at or above upper limit: 'off'
     Remove protection against out-of-range input in generated code: 'off'
   */

    /* Column-major Lookup 1-D
     Search method: 'binary'
     Use previous index: 'off'
     Interpolation method: 'Linear point-slope'
     Extrapolation method: 'Linear'
     Use last breakpoint for index at or above upper limit: 'off'
     Remove protection against out-of-range input in generated code: 'off'
   */
    /* Prelookup - Index and Fraction
     Index Search method: 'binary'
     Extrapolation method: 'Linear'
     Use previous index: 'off'
     Use last breakpoint for index at or above upper limit: 'off'
     Remove protection against out-of-range input in generated code: 'off'
   */
    if (u0 <= bp0[0U]) {
        iLeft = 0U;
        frac = (u0 - bp0[0U]) / (bp0[1U] - bp0[0U]);
    } else if (u0 < bp0[maxIndex]) {
        /* Binary Search */
        bpIdx = maxIndex >> 1U;
        iLeft = 0U;
        iRght = maxIndex;
        while (iRght - iLeft > 1U) {
            if (u0 < bp0[bpIdx]) {
                iRght = bpIdx;
            } else {
                iLeft = bpIdx;
            }

            bpIdx = (iRght + iLeft) >> 1U;
        }

        frac = (u0 - bp0[iLeft]) / (bp0[iLeft + 1U] - bp0[iLeft]);
    } else {
        iLeft = maxIndex - 1U;
        frac = (u0 - bp0[maxIndex - 1U]) / (bp0[maxIndex] - bp0[maxIndex - 1U]);
    }

    /* Column-major Interpolation 1-D
     Interpolation method: 'Linear point-slope'
     Use last breakpoint for index at or above upper limit: 'off'
     Overflow mode: 'portable wrapping'
   */
    return (table[iLeft + 1U] - table[iLeft]) * frac + table[iLeft];
}
